package rpg.core
{
	import flash.events.MouseEvent;

	import rpg.Utils.GameUtils;
	import rpg.Utils.Val;
	import rpg.avatar.AvatarComponent;
	import rpg.manager.GameManager;
	import rpg.role.Hero;
	import rpg.state.StateComponent;

	public class MouseComponent extends FrameComponent
	{
		private var role : Hero;
		private var autoCastSpellComponent : AutoCastSpellComponent;
		private var stateComponent : StateComponent;
		private var gm : GameManager;

		public function MouseComponent(type : *)
		{
			super(type);
		}

		override public function notifyAdded() : void
		{
			super.notifyAdded();
			role = owner as Hero;
			autoCastSpellComponent = role.getComponent(AutoCastSpellComponent);
			gm = GameManager.getInstance();
			stateComponent = role.getComponent(StateComponent);
			GameUtils.stage.addEventListener(MouseEvent.CLICK , onMouseClick);
		}

		private function onMouseClick(evt : MouseEvent) : void
		{
			if(gm.tmpOnMouseComponent)
			{
				var tmpSelectTarget : Hero = gm.tmpOnMouseComponent.hero;
				gm.targetSelect(tmpSelectTarget);
				if(tmpSelectTarget.data.teamID == Val.TEAM_ENEMY)
				{
					autoCastSpellComponent.attack(tmpSelectTarget);
				}
			}
			else
			{
				role.targetMapX = GameUtils.viewX + evt.localX;
				role.targetMapY = GameUtils.viewY + evt.localY;
				if(role.canWalk)
				{
					role.changeState(Val.WALK);
					autoCastSpellComponent.stopAttack();
				}
				gm.setClickAnimationPosition(role.targetMapX , role.targetMapY);
			}
		}
	}
}